package com.xc.stock.utils.jackjson;

import java.io.IOException;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

/**
 * @description: string 转json
 * @author: YUAN YONG
 * @create: 2019-08-02
 **/

public class String2JsonObjSerializer extends JsonSerializer<String> {

    @Override
    public void serialize(
            String string, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {

        if(StringUtils.isBlank(string)){
            jsonGenerator.writeNull();
        }else{
            JSONObject json = JSON.parseObject(string);
            if(json == null) {
                jsonGenerator.writeNull();
            } else {
                jsonGenerator.writeObject(json);
            }
        }

    }

}
